﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;

namespace Web.Delivery_Note
{
    public partial class AddNewDeliveryNote : System.Web.UI.Page
    {
        static string prevPage = String.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            if (ViewState["Products"] == null)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("ProductName"));
                dt.Columns.Add(new DataColumn("ProductDescription"));
                dt.Columns.Add(new DataColumn("Quantity"));
                ViewState["Products"] = AddRow(dt);
            }
        }
        private DataTable AddRow(DataTable dt)
        {
            DataRow dr = dt.NewRow();
            dr["ProductName"] = "";
            dr["ProductDescription"] = "";
            dr["Quantity"] = "";
            dt.Rows.Add(dr);
            return dt;
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {

        }

        protected void btn_Cancel_Click(object sender, EventArgs e)
        {

        }
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadGrid1.DataSource = dt;
        }
        private DataTable GetAllRow()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ProductName"));
            dt.Columns.Add(new DataColumn("ProductDescription"));
            dt.Columns.Add(new DataColumn("Quantity"));
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                TextBox txtProduct = (TextBox)item.FindControl("txtProduct");
                TextBox txtProductDescription = (TextBox)item.FindControl("txtProductDescription");
                TextBox txtQuantity = (TextBox)item.FindControl("txtQuantity");

                DataRow dr = dt.NewRow();
                dr["ProductName"] = txtProduct.Text;
                dr["ProductDescription"] = txtProductDescription.Text;
                dr["Quantity"] = txtQuantity.Text;
                dt.Rows.Add(dr);
            }
            return dt;
        }
        protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = e.Item as GridDataItem;
            if (e.CommandName == "Insert")
            {
                DataTable dt = GetAllRow();
                dt = AddRow(dt);
                ViewState["Products"] = dt;
                
                RadGrid1.DataSource = dt;
                RadGrid1.DataBind();
            }
            else if (e.CommandName == "Delete")
            {
                DataTable dt = (DataTable)ViewState["Products"];
                TextBox txt1 = (TextBox)item.FindControl("txtProduct");
                DataRow dr = null;
                foreach (DataRow r in dt.Rows)
                {
                    if (r["ProductName"].ToString() == txt1.Text)
                    {
                        dr = r;
                        break;
                    }
                }
                if (dr != null)
                    dt.Rows.Remove(dr);
                RadGrid1.DataSource = dt;
                RadGrid1.DataBind();
            }        
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                //Capture row
                GridDataItem gridRow = (GridDataItem)e.Item;
                //find text boxes
                TextBox txtProduct = (TextBox)gridRow.FindControl("txtProduct");
                TextBox txtProductDescription = (TextBox)gridRow.FindControl("txtProductDescription");
                TextBox txtQuantity = (TextBox)gridRow.FindControl("txtQuantity");
                if (txtProduct != null)
                {

                }
            }
        }
    }
}